home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / surfsrc3.zip / READFILE.INC < prev    next >
Text File  |  1991-09-28  |  12KB  |  313 lines

  1. procedure OPENFILE (var Flnm: text80; var Infile: text);
  2. { Open a file with error checking. Prompt for new one if file not found }
  3.  
  4. begin   Fileread := FALSE;
  5.   while (NOT Fileread) do begin
  6.     assign (Infile, Flnm);
  7.     {$I-}
  8.     reset (Infile);
  9.     {$I+}
  10.     if (ioresult <> 0) then begin
  11.       writeln ('Error: file ',Flnm,' does not exist.');
  12.       write ('Enter new file name (or <enter> to exit): ');
  13.       readln (Flnm);
  14.       if (Flnm = '') then
  15.         halt;
  16.     end else
  17.       Fileread := TRUE;
  18.   end;
  19. end;   { procedure OPENFILE }
  20.  
  21.  
  22.  
  23. procedure READFILE (Flnm: text80);
  24. { read the input data from the file }
  25.  
  26. var
  27.   Version: integer;       { used for multiple version input flag (only 4 now) }
  28.   j: integer;             { counter for looping and reading into arrays}
  29.   Infile: text;           { file to read}
  30.   Realvar: vartype;       { temporary array for storage of line input }
  31.   Num: integer;           { number of inputted values on the line }
  32.   Comment: text80;        { comment at end of line }
  33.   Line_num: integer;      { line number in input file }
  34.   Nvread: integer;        { #vertices read so far in this surface }
  35.   Vert: integer;          { vertex # }
  36.   Nscript: integer;       { #script inputs }
  37.   Mat: integer;           { material # }
  38.   Node: word;             { node # }
  39.   Surf: word;             { surface # }
  40.   Connection: word;       { next connection number on surface }
  41.   Command: cmmdtype;      { symbolic command type }
  42.   Parm: parmtype;         { symbolic parameter }
  43.   ch: char;
  44.  
  45. begin
  46.   clrscr;
  47.   writeln ('Reading data file . . .');
  48.  
  49.   openfile (Flnm, Infile);
  50.   readln (Infile, Flpurpose);
  51.   Line_num := 1;
  52.   Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num, FALSE);
  53.   if (Num <> 1) then begin
  54.     writeln ('Bad input: Reading version number.');
  55.     close (Infile);
  56.     halt;
  57.   end;
  58.   Version := round(Realvar[1]);
  59.   if (Version = 1) then begin
  60.     Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num, FALSE);
  61.     if (Num <> 4) then begin
  62.       writeln ('Bad input: Reading #nodes, #surfaces, Maxvert and #materials',
  63.           ' (line ',Line_num,')');
  64.       close (Infile);
  65.       halt;
  66.     end;
  67.     Nnodes := round(Realvar[1]);
  68.     Nsurf := round(Realvar[2]);
  69.     Maxvert := round(Realvar[3]);
  70.     Nmatl := round(Realvar[4]);
  71.     Nscript := 0;
  72.     Nsides := 1;
  73.   end else if (Version = 2) then begin
  74.     Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num, FALSE);
  75.     if (Num <> 6) then begin
  76.       writeln ('Bad input: Reading #matl, #nodes, #surf, #script, Maxvert,',
  77.           ' #sides (line ',Line_num,')');
  78.       close (Infile);
  79.       halt;
  80.     end;
  81.     Nmatl := round(Realvar[1]);
  82.     Nnodes := round(Realvar[2]);
  83.     Nsurf := round(Realvar[3]);
  84.     Nscript := round(Realvar[4]);
  85.     Maxvert := round(Realvar[5]);
  86.     Nsides := round(Realvar[6]);
  87.   end else if (Version = 3) or (Version = 4) or (Version = 5) then begin
  88.     Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num, FALSE);
  89.     if (Num <> 5) then begin
  90.       writeln ('Bad input: Reading #matl, #nodes, #surf, Maxvert,',
  91.           ' #sides (line ',Line_num,')');
  92.       close (Infile);
  93.       halt;
  94.     end;
  95.     Nmatl := round(Realvar[1]);
  96.     Nnodes := round(Realvar[2]);
  97.     Nsurf := round(Realvar[3]);
  98.     Maxvert := round(Realvar[4]);
  99.     Nsides := round(Realvar[5]);
  100.   end else begin
  101.     writeln('Wrong data input version number specified');
  102.     close (Infile);
  103.     halt;
  104.   end;
  105.  
  106. {$ifdef BIGMEM}
  107.   { Check for valid range }
  108.   if (Nnodes > MAXALLOC) or (Nnodes < 3) or (Nsurf * Maxvert > MAXALLOC) or
  109.       (Nsurf * Maxvert < 1) or (Maxvert < 3) then begin
  110.     writeln('Invalid Nnodes=', Nnodes, ' or Nsurf=', Nsurf,
  111.         ' or Maxvert=', Maxvert);
  112.     halt;
  113.   end;
  114.  
  115.   { Dynamically allocate enough space for all nodes & surfaces }
  116.   if (not alloc_nodes) then begin
  117.     writeln('Can not allocate sufficient memory for ', Nnodes, ' nodes.');
  118.     halt;
  119.   end;
  120.   if (not alloc_surfs) then begin
  121.     writeln('Can not allocate sufficient memory for ', Nsurf, ' surfaces.');
  122.     halt;
  123.   end;
  124.  
  125.   with ptra^ do with ptrb^ do with ptrc^ do with ptrg^ do
  126.     with ptrh^ do with ptri^ do
  127.       begin {with}
  128. {$endif}
  129.  
  130.   if (Nnodes<=MAXNODES) and (Nsurf<=MAXSURF) and (Nmatl<=MAXMATL) and
  131.        (Maxvert*Nsurf<=MAXCONNECT) and (Nsides<=2) and (Nnodes>0) and
  132.        (Nsurf>0) and (Nmatl>0) then begin
  133.     for Mat := 1 to Nmatl do begin
  134.         Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num,
  135.             FALSE);
  136.         if (Version <= 2) then begin
  137.           if (Num <> 3) then begin
  138.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  139.                 Line_num,')');
  140.             close (Infile);
  141.             halt;
  142.           end;
  143.           R1[Mat] := Realvar[1];
  144.           R2[Mat] := Realvar[2];
  145.           R3[Mat] := 0.0;
  146.           Color[Mat] := round(Realvar[3]);
  147.           Ambient[Mat] := 0.1;
  148.           color_to_RGB (Color[Mat], Redmax[Mat], Grnmax[Mat], Blumax[Mat]);
  149.         end else if (Version = 3) then begin
  150.           if (Num <> 4) then begin
  151.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  152.                 Line_num,')');
  153.             close (Infile);
  154.             halt;
  155.           end;
  156.           R1[Mat] := Realvar[1];
  157.           R2[Mat] := Realvar[2];
  158.           R3[Mat] := Realvar[3];
  159.           Color[Mat] := round(Realvar[4]);
  160.           Ambient[Mat] := 0.1;
  161.           color_to_RGB (Color[Mat], Redmax[Mat], Grnmax[Mat], Blumax[Mat]);
  162.         end else begin
  163.           { Version 4 or 5 }
  164.           if (Num <> 5) then begin
  165.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  166.                 Line_num,')');
  167.             close (Infile);
  168.             halt;
  169.           end;
  170.           R1[Mat] := Realvar[1];
  171.           R2[Mat] := Realvar[2];
  172.           R3[Mat] := Realvar[3];
  173.           Color[Mat] := round(Realvar[4]);
  174.           Ambient[Mat] := Realvar[5];
  175.           if (Version > 4) then begin
  176.             Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num,
  177.                 FALSE);
  178.             if (Num <> 6) then begin
  179.               writeln ('Bad input: Reading data for material #',Mat,' (line ',
  180.                   Line_num,')');
  181.               close (Infile);
  182.               halt;
  183.             end;
  184.             Redmax[Mat] := round(Realvar[1]);
  185.             Grnmax[Mat] := round(Realvar[2]);
  186.             Blumax[Mat] := round(Realvar[3]);
  187.             if Redmax[Mat] < 1 then
  188.               Redmax[Mat] := 1;
  189.             if Redmax[Mat] > 256 then
  190.               Redmax[Mat] := 256;
  191.             if Grnmax[Mat] < 1 then
  192.               Grnmax[Mat] := 1;
  193.             if Grnmax[Mat] > 256 then
  194.               Grnmax[Mat] := 256;
  195.             if Blumax[Mat] < 1 then
  196.               Blumax[Mat] := 1;
  197.             if Blumax[Mat] > 256 then
  198.               Blumax[Mat] := 256;
  199.           end else { if Version > 4 }
  200.             color_to_RGB (Color[Mat], Redmax[Mat], Grnmax[Mat], Blumax[Mat]);
  201.         end; { if Version }
  202.       end;  {for Mat}
  203.  
  204.       for Node := 1 to Nnodes do
  205.       begin
  206.            Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num,
  207.               FALSE);
  208.            if (Num <> 3) then
  209.            begin
  210.                 writeln ('Bad input: Reading data for node #',Node,' (line ',
  211.                 Line_num,')');
  212.                 close (Infile);
  213.                 halt;
  214.            end;
  215.            Xworld[Node] := Realvar[1];
  216.            Yworld[Node] := Realvar[2];
  217.            Zworld[Node] := Realvar[3];
  218.       end; {for Node}
  219.       for Surf := 1 to Nsurf do
  220.       begin
  221.         Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num,
  222.             FALSE);
  223.         if (Num < 5) then begin
  224.            writeln ('Bad input: Reading data for surface #',Surf,' (line ',
  225.            Line_num,')');
  226.            if (Num > 2) then
  227.               writeln ('Must have at least 3 nodes on a surface!');
  228.            close (Infile);
  229.            halt;
  230.         end;
  231.         Nvert[Surf] := round(Realvar[1]);
  232.         Matl[Surf] := round(Realvar[2]);
  233.         if (Nvert[Surf]<3) or (Nvert[Surf]>Maxvert) or (Nvert[Surf]<Num-2)
  234.            or (Matl[Surf]<1) or (Matl[Surf]>Nmatl) then begin
  235.            writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  236.            if (Nvert[Surf] < 3) then
  237.               writeln ('Must have at least 3 nodes per surface')
  238.            else if (Nvert[Surf] > Maxvert) then
  239.              writeln ('#vertices exceeds Maxvert')
  240.            else if (Matl[Surf]<1) or (Matl[Surf]>Nmatl) then
  241.              writeln ('Matl no. not in range 0..Nmatl (',Nmatl,')')
  242.            else
  243.              writeln ('#vertices specified does not match #arguments');
  244.            close (Infile);
  245.            halt;
  246.         end; { if Nvert... }
  247.         Nvread := Num - 2;
  248.         for Vert := 1 to Nvread do begin
  249.           Connection := round(Realvar[Vert+2]);
  250.           if (Connection<1) or (Connection>Nnodes) then begin
  251.             writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  252.             writeln ('Connection #,',Vert,' not in range 0..Nnodes (',
  253.               Nnodes,')');
  254.             close (Infile);
  255.              halt;
  256.           end;
  257.           Connect[(Surf-1)*Maxvert+Vert] := Connection;
  258.         end; { for Vert }
  259.         while (Nvread < Nvert[Surf]) do begin
  260.           Num := inreal (Infile, Realvar, Comment, Command, Parm, Line_num,
  261.               FALSE);
  262.           if (Num < 1) or (Nvread + Num > Nvert[Surf]) then begin
  263.             writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  264.             if (Num = 0) then writeln ('No data read.')
  265.             else if (Nvread + Num > Nvert[Surf]) then
  266.               writeln ('Too many vertices read.');
  267.             close (Infile);
  268.             halt;
  269.           end; { if Num... }
  270.           Vert := Nvread + 1;
  271.           for j := 1 to Num do begin
  272.             Connection := round(Realvar[j]);
  273.             if (Connection<1) or (Connection>Nnodes) then begin
  274.               writeln ('Error in surface ',Surf,'(line ', Line_num,'): ');
  275.               writeln ('Connection #,',Vert,
  276.                        ' not in range 0..Nnodes (',Nnodes,')');
  277.               close (Infile);
  278.               halt;
  279.             end;
  280.             Connect[(Surf-1)*Maxvert+Vert] := Connection;
  281.             Vert := Vert + 1;
  282.           end;
  283.           Nvread := Nvread + Num;
  284.         end; { while }
  285.       end; { for Surf }
  286.     end
  287.     else begin
  288.       if (Nnodes>MAXNODES) or (Nnodes<1) then
  289.         writeln('Nnodes (',Nnodes,') must be between 1 and ',MAXNODES);
  290.       if (Nsurf>MAXSURF) or (Nsurf<1) then
  291.         writeln('Nsurf (',Nsurf,') must be between 1 and ',MAXSURF);
  292.       if (Nmatl>MAXMATL) or (Nmatl<1) then
  293.         writeln('Nmatl (',Nmatl,') must be between 1 and ',MAXMATL);
  294.       if Maxvert*Nsurf>MAXCONNECT then begin
  295.         writeln('Number of surfaces or max number of vertices too large!');
  296.         writeln('Maxvert (',Maxvert,') * Nsurf (',Nsurf,
  297.                 ') must be smaller than ',MAXCONNECT);
  298.       end;
  299.       if (Nsides<1) or (Nsides>2) then
  300.         writeln('Nsides (',Nsides,') must be either 1 or 2');
  301.       close (Infile);
  302.       halt;
  303.     end; { if Nnodes... }
  304.  
  305.     close (Infile);
  306.  
  307.     readcfg (Flnm, FALSE);
  308.  
  309. {$ifdef BIGMEM}
  310.   end; {with}
  311. {$endif}
  312. end; { procedure READFILE }
  313.